
function onclick_newPost() {
	resetInput();
	$("#dialog-form").dialog("open");
}

function resetInput() {
	document.getElementById("titleName").value = "";
	document.getElementById("description").value = "";
	document.getElementById("context").value = "";
}

function inital() {
	$("#dialog-form").dialog({
		autoOpen : false,
		draggable : false,
		show : "slide",
		hide : "slide",
		modal : true,
		width : 500,
		resizable : false,
		position : [ "center", 50 ],
		buttons : {
			"PUBLISH" : function() {
				if (checkFormValid()) {
					$(this).dialog("close");
					// alert("Ready To Published!");
					sendPostToServer();
				} else {
					alert("Please fill the all field first!");
				}
			},
			"CANCEL" : function() {
				$(this).dialog("close");
			}
		}
	});
}

function checkFormValid() {
	var titleName = $("#titleName").val();
	var description = $("#description").val();
	var context = $("#context").val();
	var authenSelect = $("#authenSelect").val();

	if (titleName != "" && description != "" && context != ""
			&& authenSelect != "") {
		return true;
	} else {
		return false;
	}
}

function sendPostToServer() {
	var titleName = $("#titleName").val();
	var description = $("#description").val();
	var context = $("#context").val();

	var PD = {
		"titleName" : titleName,
		"description" : description,
		"context" : context
	};

	$.ajax({
		type : "POST",
		url : "../StorePostInDB",
		data : PD,
		success : callbackPD,
		dataType : "json"
	});
	// alert("ajax end");
	// w@123.com+d+12+undefined+Engineer+10/08/1989+on
}

function callbackPD(data) {
	// alert("callbackPD");
	if (data.result) {
		alert("Congratulations!\nThe post is published!");
		//addNewTrToTable();
		window.location.reload();
	} else {
		window.location.href = "../WrongPage.jsp";
	}
}

function addNewTrToTable() {
	var i = $("#tblPostList")[0].rows.length+1;
	var content = "<tr>" + "<td>" + i + "</td>" + "<td>"
			+ $("#titleName").val() + "</td>" + "<td>"
			+ $("#description").val() + "</td>" + "<td>" + "2222" + "</td>"
			+ "<td>" + "owner" + "</td>" + "<td>"
			+ "<a href=\"Forum.jsp?postID=" + "1244" + "\">See Details</a>"
			+ "</td>" + "</tr>"
	$("#tblPostList").append(content);
}